import { Body, Controller, Get, Param, Post, UseGuards, Headers } from '@nestjs/common';
import { DiaryChatService } from './diaryChat.service';
import { ChatDiaryDto, DiaryPolishDto, ChatDiaryAfterDto } from './dto/diaryChat.dto';
import { ERROR_CODE, ERROR_MESSAGE, getResponse, WEATHER_TYPE } from 'src/common/const/const';
import { AuthService } from 'src/services/Auth';
import { VipUsageService } from 'src/vipUsage/vipUsage.service';
import { VipInfoService } from 'src/vipInfo/vipInfo.service';
import { NonVipAiChatLimit } from 'src/common/const/const';

@Controller('chat')
export class DiaryController {
  constructor(
    private readonly diaryService: DiaryChatService,
    private readonly authService: AuthService,
    private readonly vipInfoService: VipInfoService,
    private readonly vipUsageService: VipUsageService
  ) { }

  @Post('/chatDiary')
  async charDiary(@Body() chatDiaryDto: ChatDiaryDto, @Headers() headers: any) {
    const userInfo = this.authService.checkAuth(headers);
    if (!userInfo) {
      return getResponse(ERROR_CODE.VERIFY_EXPIRE, ERROR_MESSAGE[ERROR_CODE.VERIFY_EXPIRE], {})
    }

    const userid = userInfo.userid
    const isVip = await this.vipInfoService.isVipUser(userid)
    // 非 vip 需要检查，该功能是否已经超过限制
    if (!isVip) {
      const chatDiaryCnt = await this.vipUsageService.getDiaryChatCnt(userid);
      const visualChatCnt = await this.vipUsageService.getVisualChatCnt(userid);
      const aiChatCnt = chatDiaryCnt + visualChatCnt;
      if (aiChatCnt > NonVipAiChatLimit) {
        return getResponse(ERROR_CODE.VIP_USAGE_COUNT_REACH_LIMIT, ERROR_MESSAGE[ERROR_CODE.VIP_USAGE_COUNT_REACH_LIMIT], {})
      }
    }

    let response = await this.diaryService.charDiary({ userid: userid, ...chatDiaryDto });
    if (response && response == "") {
      return getResponse(0, ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL]);
    }
    const textMsg = {
      type: "text",
      content: response
    }
    // 更新 vip 功能使用次数
    await this.vipUsageService.addDiaryChatCnt(userid)
    return getResponse(0, "success", [textMsg]);
  }

  @Post('/diaryPolish')
  async diaryPolish(@Body() diaryPolishDto: DiaryPolishDto, @Headers() headers: any) {
    const userInfo = this.authService.checkAuth(headers);
    if (!userInfo) {
      return getResponse(ERROR_CODE.VERIFY_EXPIRE, ERROR_MESSAGE[ERROR_CODE.VERIFY_EXPIRE], {})
    }
    let response = await this.diaryService.diaryPolish({ userid: userInfo.userid, ...diaryPolishDto });
    // 增加返回天气信息 todo 根据经纬度获取天气信息
    let result = {
      data: response || "",
      // 天气信息
      weather: WEATHER_TYPE.SUNNY
    }

    return getResponse(0, "success", result);
  }

  @Post('/chatDiaryAfter')
  async chatDiaryAfter(@Body() chatDiaryAfterDto: ChatDiaryAfterDto, @Headers() headers: any) {
    const userInfo = this.authService.checkAuth(headers);
    if (!userInfo) {
      return getResponse(ERROR_CODE.VERIFY_EXPIRE, ERROR_MESSAGE[ERROR_CODE.VERIFY_EXPIRE], {})
    }

    let response = await this.diaryService.charDiaryAfter({ content: "这是我今天发生的事，你有什么要跟我说的吗?", userid: userInfo.userid, ...chatDiaryAfterDto });
    if (response && response == "") {
      return getResponse(0, ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL]);
    }
    const textMsg = {
      type: "text",
      content: response
    }
    // const imgMsg = {
    //   type: "img",
    //   content: "https://pic.ibaotu.com/02/08/24/10t888piC68n.jpg%21w280"
    // }
    return getResponse(0, "success", [textMsg]);
  }

}
